package com.qf.petmall.starter.square.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.petmall.starter.square.entity.Imgurl;
import com.qf.petmall.starter.square.entity.Square;
import com.qf.petmall.starter.square.service.IImgurlService;
import com.qf.petmall.starter.square.service.ISquareService;
import com.qf.petmall.starter.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 广场发布的页面模块 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-12-06
 */
@CrossOrigin
@RestController
@RequestMapping("/square")
public class SquareController {

    @Autowired
    IImgurlService iImgurlService;
    @Autowired
    ISquareService iSquareServicel;

    @GetMapping("/del/{id}")
    public R del( @PathVariable("id") Integer id ) {
        boolean b=iSquareServicel.removeById( id );
        LambdaQueryWrapper< Imgurl > imgurlLambdaQueryWrapper=new LambdaQueryWrapper< Imgurl >( );
        imgurlLambdaQueryWrapper.eq( Imgurl::getParentId,id );
        iImgurlService.remove( imgurlLambdaQueryWrapper );
        return R.ok( ).put( "code","200" );
    }

    @GetMapping("/getbyclass/{sss}")
    public R getbyclass( @PathVariable("sss") String sss ) {
        List< Square > list=iSquareServicel.list( );
        List< Imgurl > list1=iImgurlService.list( );
        for(Square square: list) {
            ArrayList< Imgurl > imgurls=new ArrayList<>( );
            for(Imgurl imgurl: list1) {
                if(imgurl.getParentId( )==square.getId( )) {
                    imgurls.add( imgurl );
                }
            }
            square.setImgurls( imgurls );
        }
        if(sss.equals( "all" )) {
            return R.ok( ).put( "ok",list );

        }else {
/*            List<String> collect1 = list.stream().map(o -> o.getClass1()).collect(Collectors.toList());
            for (String s : collect1) {
                System.out.println(s);
            }*/
            List< Square > collect=list.stream( ).filter( o->sss.equals( o.getClass1( ) ) ).collect( Collectors.toList( ) );
            return R.ok( ).put( "ok",collect );
        }


    }

    @PostMapping("/add")
    public R add( @RequestBody Square square ) {

        iSquareServicel.save( square );

        List< Imgurl > imgurls=square.getImgurls( );

        for(Imgurl imgurl: imgurls) {
            imgurl.setParentId( square.getId( ) );
        }
        iImgurlService.saveBatch( imgurls );
        return R.ok( ).put( "code","200" );
    }

    @PostMapping("/update")
    public R update( @RequestBody Square square ) {
        iSquareServicel.updateById( square );
        List< Imgurl > imgurls=square.getImgurls( );
        for(Imgurl imgurl: imgurls) {
            imgurl.setParentId( square.getId( ) );
        }
        List< Integer > collect=imgurls.stream( ).map( o->o.getParentId( ) ).collect( Collectors.toList( ) );
        List< String > collect1=imgurls.stream( ).map( o->o.getImg( ) ).collect( Collectors.toList( ) );
        LambdaUpdateWrapper< Imgurl > imgurlLambdaUpdateWrapper=new LambdaUpdateWrapper<>( );
        imgurlLambdaUpdateWrapper.in( Imgurl::getParentId,collect ).set( Imgurl::getImg,collect1 );
        iImgurlService.update( imgurlLambdaUpdateWrapper );
        return R.ok( ).put( "code","200" );

    }


    @PostMapping("/adddianzan")
    public R dianzan( @RequestBody Square square ) {
        Square servicelById=iSquareServicel.getById( square.getId( ) );

        servicelById.setDianzan( !square.isDianzan( ) );

        iSquareServicel.updateById( servicelById );
        return R.ok( ).put( "square",servicelById );
    }


    @PostMapping("/addpinglun")
    public R addpinglun( @RequestBody Square square ) {
        Square square1=iSquareServicel.getById( square.getId( ) );

        Square setPinglun=square1.setPinglun( square.getPinglun( ) );

        iSquareServicel.updateById( setPinglun );

            return R.ok( ).put( "square",setPinglun );
    }


    @GetMapping("/test")
    public R test(){

        List< Square > list=iSquareServicel.list( );

        return R.ok().put("a",list);
    }
}
